package com.moxiao.yfjh.module.activity.controller.app;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.module.activity.controller.admin.merchantInfo.vo.*;
import com.moxiao.yfjh.module.activity.convert.merchantInfo.MerchantInfoConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchanType.MerchantTypeDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchantInfo.MerchantInfoDO;
import com.moxiao.yfjh.module.activity.service.merchanType.MerchantTypeService;
import com.moxiao.yfjh.module.activity.service.merchantInfo.MerchantInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.error;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "app - 相亲商家信息")
@RestController
@RequestMapping("/app/merchant/info")
@Validated
public class AppMerchantInfoController {

    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private MerchantTypeService merchantTypeService;


    @GetMapping("/get")
    @Operation(summary = "获得相亲商家信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:query')")
    public CommonResult<MerchantInfoRespVO> getMerchantInfo(@RequestParam("id") Long id) {
        MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(id);
        MerchantInfoRespVO merchantInfoRespVO = MerchantInfoConvert.INSTANCE.convert(merchantInfo);
        MerchantTypeDO merchantType = merchantTypeService.getMerchantType(merchantInfo.getMerchantType());
        if (Objects.nonNull(merchantType)) {
            merchantInfoRespVO.setMerchantTypeName(merchantType.getName());
        }
        return success(merchantInfoRespVO);
    }

    @GetMapping("/getInfo")
    @Operation(summary = "商家登录-获得相亲商家信息")
    public CommonResult<MerchantInfoRespVO> getInfo() {
        Long loginUserId = getLoginUserId();
        MerchantInfoDO merchantInfo = merchantInfoService.getLongin(loginUserId);
        if (Objects.isNull(merchantInfo)) {
            error(401, "获取商家信息失败！请联系管理员");
        }
        MerchantInfoRespVO merchantInfoRespVO = MerchantInfoConvert.INSTANCE.convert(merchantInfo);
        MerchantTypeDO merchantType = merchantTypeService.getMerchantType(merchantInfo.getMerchantType());
        if (Objects.nonNull(merchantType)) {
            merchantInfoRespVO.setMerchantTypeName(merchantType.getName());
        }
        return success(merchantInfoRespVO);
    }
}
